package com.lsh.oms.core.service.status.impl;

import com.lsh.base.common.json.JsonUtils;
import com.lsh.oms.core.enums.*;
import com.lsh.oms.core.model.order.OrderHead;
import com.lsh.oms.core.model.order.OrderShippingHead;
import com.lsh.oms.core.model.order.OrderShippingHeadWithBLOBs;
import com.lsh.oms.core.model.order.OrderSignHead;
import com.lsh.oms.core.proxy.service.EsProxy;
import com.lsh.oms.core.service.order.OrderUpdateService;
import com.lsh.oms.core.service.other.ShippingOrderService;
import com.lsh.oms.core.service.other.SignOrderService;
import com.lsh.oms.core.service.status.BillAdapterService;
import com.lsh.oms.core.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 2020-02-17
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service
@Slf4j
public class BillAdapterServiceImpl implements BillAdapterService {

    @Autowired
    private OrderUpdateService orderUpdateService;

    @Autowired
    private ShippingOrderService shippingOrderService;

    @Autowired
    private SignOrderService signOrderService;

    @Autowired
    private EsProxy esProxy;


    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean reNew(Long orderCode, OrderHead entity, OrderOperateType orderOperateType, Long venderId) {

        boolean updateFlag =  orderUpdateService.updateOrderStatus(orderCode,entity,orderOperateType,venderId) >= 1;
        OrderShippingHead orderShippingHead4update = new OrderShippingHead();
        OrderSignHead orderSignHead4update = new OrderSignHead();
        if(updateFlag){
            OrderShippingHead orderShippingHead = shippingOrderService.selectOneByOrderCode(orderCode,false);

            orderShippingHead4update.setShippingOrderId(orderShippingHead.getShippingOrderId());
            orderShippingHead4update.setActivedAt(DateUtil.getCurrentTime());
            orderShippingHead4update.setStatus(ShippingStatus.DELIVERING.getIndex());
            orderShippingHead4update.setReceiptStatus(ShippingReceiptStatus.UN_RECEIPT.getIndex());

            updateFlag = shippingOrderService.updateStatus(orderShippingHead4update);

            if(updateFlag){
                OrderSignHead orderSignHead = signOrderService.getSignHeadByOrderId(orderCode,false);

                orderSignHead4update.setReceiptOrderId(orderSignHead.getReceiptOrderId());

                orderSignHead4update.setStatus(SignStatus.NOT_ARRIVAL.getIndex());
                orderSignHead4update.setPayStatus(PayStatus.NOT_PAID.getValue());
                orderSignHead4update.setPayType(PayChannel.NO.getIndex());
                orderSignHead4update.setConfirmAt(DateUtil.getCurrentTime());
                orderSignHead4update.setIsValid(Valid.DISABLE.getValue());

                updateFlag = signOrderService.updateArrivalStatus(orderSignHead4update,"");
            }
        }

        if(updateFlag){
            this.sendEs4order(orderCode,OrderStatus.ORDER_DELIVERY.getIndex(),"");
            this.sendEs4shipping(orderShippingHead4update,"");
            this.sendEs4sigh(orderSignHead4update,"");
        }

        return updateFlag;
    }


    /**
     * 更新订单es
     *
     * @param orderCode 参数
     * @param logSign   参数
     */
    private void sendEs4order(Long orderCode, Integer orderStatus, String logSign) {
        OrderHead orderHead = new OrderHead(orderCode, orderStatus);
        esProxy.editIndex(EsProxy.transformToOrderHeadForEs(orderHead), logSign);
    }

    /**
     * 更新发货es
     *
     * @param shippingHead 参数
     * @param logSign      参数
     */
    private void sendEs4shipping(OrderShippingHead shippingHead, String logSign) {
        OrderShippingHeadWithBLOBs oshEs = new OrderShippingHeadWithBLOBs();
        oshEs.setShippingOrderId(shippingHead.getShippingOrderId());
        oshEs.setStatus(shippingHead.getStatus());
        log.info(logSign + "ES入参修改发货是-- " + JsonUtils.obj2Json(oshEs));
        esProxy.editIndex(oshEs, logSign);
    }

    /**
     * 更新发货es
     *
     * @param orderSignHead 参数
     * @param logSign      参数
     */
    private void sendEs4sigh(OrderSignHead orderSignHead, String logSign) {
        OrderSignHead oshEs = new OrderSignHead();
        oshEs.setReceiptOrderId(orderSignHead.getReceiptOrderId());
        oshEs.setIsValid(orderSignHead.getIsValid());
        log.info(logSign + "ES入参修改签收是-- " + JsonUtils.obj2Json(oshEs));
        esProxy.editIndex(oshEs, logSign);
    }
}
